package com.example.shopping.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.shopping.common.consts.MessageEnum;
import com.example.shopping.dao.CarDao;
import com.example.shopping.dao.GoodsDao;
import com.example.shopping.pojo.domain.CarBean;
import com.example.shopping.pojo.domain.GoodsBean;
import com.example.shopping.pojo.vo.GoodsVo;
import com.example.shopping.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Dream
 * @version 1.0
 * @date 2020/11/9 13:01
 */

@Service
public class CarServiceImpl extends ServiceImpl<CarDao,CarBean> implements CarService {

    @Autowired
    private CarDao carDao;
    @Autowired
    private GoodsDao goodsDao;

    @Override
    public List<GoodsVo> getByXh(String userXh) {
        return carDao.getByXh(userXh);
    }

    @Override
    public String updateNum(Integer num, Integer carId) {
        carDao.updateNum(num,carId);
        return MessageEnum.CAR_NUM_UPDATE;
    }

    @Override
    public String addCart(CarBean carBean) {
        CarBean car=carDao.getBy(carBean.getUserXh(),carBean.getGoodsId());

        GoodsBean goodsBean=goodsDao.getById(carBean.getGoodsId());
        //浏览量+1
        goodsBean.setBrowse(goodsBean.getBrowse()+1);
        goodsDao.updateGoods(goodsBean);
        if(car==null){
            carDao.addCart(carBean);
            return MessageEnum.CAR_ADD_SUCCESS;
        }else{
            //购物车数量添加
            carDao.updateNum(car.getCarNum()+carBean.getCarNum(),car.getId());
            return MessageEnum.CAR_EXIST;
        }


    }
}
